﻿Imports System.Data.Linq
Imports System.Linq
Imports TNCDAL

Public Class MTrucks
    Private db As New TanNamChinhDataContext

    Public Function GetContList() As IQueryable(Of tblTruck)
        Try
            Dim query = From b In db.tblTrucks
                        Where b.IsActive = True And b.Type = 2
                        Order By b.TruckNumber
                        Select b

            Return query
        Catch ex As Exception
            Throw ex
        End Try
    End Function

    Public Function GetContListByName(ByVal Name As String) As IQueryable(Of tblTruck)
        Try
            Dim query = From b In db.tblTrucks
                        Where b.IsActive = True And b.Type = 2 And b.TruckNumber.ToLower.Contains(Name.ToLower)
                        Order By b.TruckNumber
                        Select b

            Return query
        Catch ex As Exception
            Throw ex
        End Try
    End Function

    Public Function getTrucksList1() As Table(Of tblTruck)
        Try
            Return db.tblTrucks
        Catch ex As Exception
            Throw ex
        End Try
    End Function

    Public Function getTrucksList2() As IQueryable(Of tblTruck)
        Try

            Dim query = From b In db.tblTrucks
                        Where b.IsActive = True
                        Order By b.ID_Truck Descending
                        Where b.Type = 1
                        Select b

                'Return (From r In db.tblCurrencies
                '           Select r).Single
            Return query
        Catch ex As Exception
            Throw ex
        End Try
    End Function
    Public Function getTrucksList2(ByVal truckNo As String) As IQueryable(Of tblTruck)
        Try

            Dim query = From b In db.tblTrucks
                        Where b.IsActive = True And b.TruckNumber.Contains(truckNo)
                        Order By b.ID_Truck Descending
                        Where b.Type = 1
                        Select b

            'Return (From r In db.tblCurrencies
            '           Select r).Single
            Return query
        Catch ex As Exception
            Throw ex
        End Try
    End Function
    Public Function getTrucksList3() As IQueryable
        Try

            Dim query = From b In db.tblTrucks
                  Select New With {b.ID_Truck, b.TruckName, b.TruckNumber}

            Return query
        Catch ex As Exception
            Throw ex
        End Try
    End Function
    Public Sub InsertNewRecord(ByVal obj As tblTruck)
        db.tblTrucks.InsertOnSubmit(obj)
        'db.GetTable(Of tblTruck)().InsertOnSubmit(obj)
        db.SubmitChanges()
    End Sub
    Public Sub UpdateRecord(ByVal obj As tblTruck)
        Dim getData = (From b In db.tblTrucks
                                Where b.ID_Truck = obj.ID_Truck
                                Select b).Single()
        getData.TruckName = obj.TruckName
        getData.TruckNumber = obj.TruckNumber
        '  getData.ID_Driver = obj.ID_Driver

        db.SubmitChanges()

    End Sub
    Public Sub UpdateDeletedRecord(ByVal obj As tblTruck)
        Dim getData = (From b In db.tblTrucks
                                Where b.ID_Truck = obj.ID_Truck
                                Select b).Single()
        ' getData.TruckName = obj.TruckName
        ' getData.TruckNumber = obj.TruckNumber
        getData.IsActive = False
        '  getData.ID_Driver = obj.ID_Driver

        db.SubmitChanges()

    End Sub
    Public Sub UpdateDeletedRecord(ByVal id As Integer)
        Dim getData = (From b In db.tblTrucks
                                Where b.ID_Truck = id
                                Select b).Single()
        ' getData.TruckName = obj.TruckName
        ' getData.TruckNumber = obj.TruckNumber
        getData.IsActive = False
        '  getData.ID_Driver = obj.ID_Driver

        db.SubmitChanges()

    End Sub
    Public Sub DeleteRecord(ByVal id As Integer)
        Dim getData = (From b In db.tblTrucks
                                Where b.ID_Truck = id
                                Select b).Single()
        db.tblTrucks.DeleteOnSubmit(getData)
        db.SubmitChanges()
    End Sub
   
End Class
